package com.tool;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
@Aspect
public class TranseactionManger {
    @Pointcut("execution(public * com.service.*.*(..))")
    private void pt1(){};
    @Autowired
    private Connecttool connecttool;


    public void setConnecttool(Connecttool connecttool) {
        this.connecttool = connecttool;
    }
    @Before(value = "pt1()")
    public void begin(){
        try {
            System.out.println("Before");
            connecttool.gettreadconnect().setAutoCommit(false);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //@AfterReturning("pt1()")
    public void commit(){
        try {
            System.out.println("AfterReturning");
            connecttool.gettreadconnect().commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @AfterThrowing("pt1()")
    public void rollback(){
        try {
            System.out.println("AfterThrowing");
            connecttool.gettreadconnect().rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //@After("pt1()")
    public void release(){
        try {
            connecttool.gettreadconnect().close();
            connecttool.removeconnect();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
